
/* Config validation */
ko.validation.configure({
    registerExtenders: true,
    messagesOnModified: true,
    insertMessages: true,
    parseInputAttributes: true,
    messageTemplate: null,
    decorateElement: true,
    errorClass: "error",
    errorMessageClass: "error-desc"
});

var mustEqual = function (val, other) {
    return val == other();
};
$(document).ready(function () {
    $(".grouped-form input").removeClass("text-active");
    $(".grouped-form input").click(function () {
        $(this).addClass("text-active");
    });
    $(".grouped-form input").blur(function () {
        $(this).removeClass("text-active");
    });

    if ($("#errormsg span").text() == "") {
        $("#errormsg").css("display", "none");
    } else {
        $("#errormsg").css("display", "block");
    }

    $(".btn-cancel-page").click(function () {

        if (isEditUserForm == false) {
            window.parent.location = "/Dashboard";
        } else {
            window.parent.location = "/User";
        }
    })
    if (data.WorkPhoneExt != "") {
        $("#WorkPhoneExt").val(data.WorkPhoneExt);
        $("#WorkPhoneExt").removeClass("text phone-ext text-active").addClass("text");
        $("#WorkPhoneExt").css({ 'width': '14%', 'min-width': '29px', 'float': 'right', 'display': 'block' });
    }
});

if ($("#hiddworkPhone .error-desc").length == 0) {
    $("#hiddworkPhone").css("display", "none");
}

//var rege = /[0-9]+[a-zA-Z]|[a-zA-Z]+[0-9]+$/;

var focusWorkPhone = false;

var agentViewModel = new function () {
    this.FirstName = ko.observable(data.FirstName).extend({ maxLength: 20 }).extend({ required: { message: GetRequiredFormat("First Name")} }).extend({ pattern: {
        message: GetInvalidFormat('First Name'),
        params: PARAM_NAME
    }
    });
    this.LastName = ko.observable(data.LastName).extend({ maxLength: 30 }).extend({ required: { message: GetRequiredFormat("Last Name")} }).extend({ pattern: {
        message: GetInvalidFormat('Last Name'),
        params: PARAM_NAME
    }
    });
    this.Email = ko.observable(data.Email).extend({ maxLength: 100 }).extend({ required: { message: GetRequiredFormat("Email")} }).extend({ pattern: {
        message: GetInvalidFormat('email'),
        params: PARAM_EMAIL
    }
    });
    this.WorkPhoneExt = ko.observable();
    var extDefault = 'Ext.';
    if (data.WorkPhoneExt != "") {
        //this.WorkPhoneExt = ko.observable(data.WorkPhoneExt);
        //$("#WorkPhoneExt").val(data.WorkPhoneExt);
    } else {
        this.WorkPhoneExt(extDefault);
        $("#WorkPhoneExt").attr("value", extDefault);
    }

    if (data.WorkPhone) {
        this.CellPhone1 = ko.observable(data.WorkPhone.substring(0, 3));
        this.CellPhone2 = ko.observable(data.WorkPhone.substring(3, 6));
        this.CellPhone3 = ko.observable(data.WorkPhone.substring(6, 10));
    } else {
        this.CellPhone1 = ko.observable('');
        this.CellPhone2 = ko.observable('');
        this.CellPhone3 = ko.observable('');
    }

    this.WorkPhone = ko.computed(function () {
        return this.CellPhone1() + this.CellPhone2() + this.CellPhone3();
    }, this).extend({ minLength: { params: 10, message: "Enter a valid phone number" },
        maxLength: { params: 10, message: "Enter a valid phone number" },
        required: { message: "Phone number is required" }
    }).extend({ phoneUS: { message: "Enter a valid phone number"} });

    // Each input
    $(":input").focus(function () {
        var id = $(this).attr("id");
        // group 1
        if (id == "cell1" || id == "cell2" || id == "cell3") {
            $("#cell1").parent('.input-holder-setup').find('.error-desc').remove();
            var message = $(this).parents(".groupinput").find('.error-desc');
            // group message
            if (focusWorkPhone) {
                message.show();
            }
            else {
                message.hide();
            }
        }

        if (id == "WorkPhoneExt") {
            if ($("#WorkPhoneExt").val() == extDefault) {
                $("#WorkPhoneExt").val("");
            }
        }
    });

    $(":input").focusout(function () {
        var id = $(this).attr("id");
        var phone = $('#WorkPhone').val();
        if (id == "cell1" || id == "cell2" || id == "cell3") {
            $("#cell1").parent('.input-holder-setup').find('.error-desc').remove();
            var message = $(this).parents(".groupinput").find('.error-desc');
            // group message
            if (phone.length == 10 && message.text() == '') {
                message.hide();
                focusWorkPhone = true;
            }
            else {
                message.show();
                focusWorkPhone = false;
            }
        }

        if (id == "WorkPhoneExt") {
            if (($("#WorkPhoneExt").val() == null) || ($("#WorkPhoneExt").val() == '')) {
                $("#WorkPhoneExt").val(extDefault);
            }
        }
    });

    this.NewPassword = ko.observable("")
		.extend({ minLength: 6, maxLength: 20 })
        .extend({ pattern: { message: 'Password must follow stated requirements.', params: '^(?=.{6})(?=.*[a-zA-Z]{1})(?=.*[0-9]{1}).*$'} });
    this.ConfirmPassword = ko.observable("")
        .extend({ pattern: { message: 'Password must follow stated requirements.', params: '^(?=.{6})(?=.*[a-zA-Z]{1})(?=.*[0-9]{1}).*$'} });
    this.CurrentPassword = ko.observable();

    // Submit function
    this.UpdateAgent = function () {
        // If current password is null or empty
        // Submit form without update/ change password

        if (agentViewModel.errors().length == 0) {
            if ($("#CurrentPasswordvalue").val() == '' && $("#NewPasswordvalue").val() == '' && $("#ConfirmPasswordvalue").val() == '') {
                return true;
            }
            else {
                if ($("#CurrentPasswordvalue").val() == '' && $("#NewPasswordvalue").val() != '') {
                    $('#CurrentPasswordvalue').parent().find('.error-desc').attr("style", "").text("Enter current password.");
                    $('#CurrentPasswordvalue').attr("class", "text text-active error");
                    return false;
                }
                return true;
                // Case 2:
            }
        }
        this.errors.showAllMessages();
        return false;
    }
};
agentViewModel.ConfirmPassword = ko.observable("").extend({
    validation: { validator: mustEqual, message: 'The two passwords entered do not match.', params: agentViewModel.NewPassword }
}),
agentViewModel.errors = ko.validation.group(agentViewModel);
ko.validation.rules.pattern.message = 'Invalid.';
ko.applyBindings(agentViewModel, document.getElementById("agent"));